package datastruct.algorithm;

public class BinarySearch {
    int[] data;
    public int binarySearch(int[] data, int num){
        int low=0, high = data.length;
        while (low < high){
            int mid = low + (high - low) / 2;
            if(data[mid] < num){
                low = mid + 1;
            }else{
                high = mid;
            }
        }
        return low;
    }


    public int addNum(int num) {
        int start = 0, end = this.data.length - 1, mid, midd;
        while (start <= end) {
            mid = (start + end) / 2;
            midd = this.data[mid];
            if (num == midd) {
                start = mid;
                break;
            } else if (num > midd) {
                start = mid + 1;
            } else {// num<nums.get(mid)
                end = mid - 1;
            }
        }
        return start;
    }

    public static void main(String[] args) {
        BinarySearch s = new BinarySearch();
        int[] a = {10,12,14,16};
        s.data = a;
        int target = 15;
        System.out.println(s.binarySearch(a, target));
        System.out.println(s.addNum(target));
    }
}
